
from lib.validator import Validator
from lib.form import Form
from model.category import CategoryDao

#import shutil    
#import os



class CategoryService():
    def __init__(self):
        self.dao = CategoryDao()
        
    
    def get_categories(self, query={}):
        _categories = self.dao.find(query)
        cats = []
        for cat in _categories:
            cats.append({'name':cat.name,
                     'description': cat.description,
                     'parent':None})
        return cats


    def submit_category(self, inputs):
        # Submit category
        if inputs['mode'] == 'edit':
            #category = category_dao.update_category(inputs['id, inputs['name, inputs['description)
            return None
        elif inputs['mode'] == 'new':

            _dict = {'name':inputs['name'],
                     'description': inputs['description']}
                
            return self.dao.save(_dict)
        '''
        if form.action == 'edit':
            category = category_dao.update_category(form.id, form.name, form.description)
        else:
            _dict = {'name':form.name,'description':form.description}
            parent = category_dao.find_one({'name':form.parent})
            if parent is not None:
                _dict['parent'] = DBRef('addresses', parent._id)
            else:
                _dict['parent'] = None
            category_dao.save(_dict)
        '''
       
class CategoryValidator(Validator):
    def __init__(self):
        Validator.__init__(self)
              
        self.rules = {'name': { 
                                'required' : [None, True],
                                'minlength':[None, 1],
                                'maxlength':[None, 256]
                                },
                      'description': { 
                                'maxlength':[None, 256]
                                }
        }

    
class CategoryForm(Form):
    """ Submit user form
    """
    def __init__(self):
        """Only accept POST request
        """
        Form.__init__(self)
        self.validator = CategoryValidator()
        
        if self.is_submitted():
            inputs = self.get_raw_inputs()
            self.errors = self.validator.validate(inputs)
            if self.errors == []:
                self.name = self.request.values.get('name')
                self.description = self.request.values.get('description')
                #self.parent = self.request.values.get('parent')
                
    def has_error(self):
        return self.errors == []
                

            
'''
class AdvertisementService():
    def __init__(self):
        self.ad_dao = AdvertisementDao()
        
        
    def get_all_infos(self):
        return self.get_infos()
    
    def get_infos(self, query={}):
        picture_dao = PictureDao()
        category_dao = CategoryDao()
        
        ads = self.ad_dao.find(query)
        infos = []
        for ad in ads:
            category = category_dao.find_one({'_id':ad.category._id})
            ItemDaoClass = self.ad_dao.items[category.name][1]
            dao = ItemDaoClass()
            item = dao.find_one({'_id':ad.item._id})

            picture = picture_dao.find_one({"item.$id":ad.item._id, "order":"0"})
            
            picture_service = PictureService(ad.user.username, category.name, item._id)
            if picture is None:
                picture.fpath = picture_service.get_picture_path(None)
            else:
                picture.fpath = picture_service.get_picture_path(picture.fname)
            
            infos.append({'ad':ad, 'item':item, 'picture':picture})
        return infos
'''